package com.example.javapracticaltraining.controller;

import com.example.javapracticaltraining.entity.R;
import com.example.javapracticaltraining.entity.pojo.AiHealthWarning;
import com.example.javapracticaltraining.entity.pojo.Elderly;
import com.example.javapracticaltraining.entity.vo.response.AiHealthWarningVO;
import com.example.javapracticaltraining.service.AiHealthWarningService;
import com.example.javapracticaltraining.service.ElderlyService;
import com.example.javapracticaltraining.service.SmartHealthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Tag(name = "AI健康预警", description = "提供AI健康风险预警分析功能")
@RestController
@RequestMapping("/ai/healthwarning")
public class AiHealthWarningController {

    @Autowired
    private AiHealthWarningService aiHealthWarningService;

    @Autowired
    private ElderlyService elderlyService;

    @Operation(summary = "获取健康风险预警", description = "分析并返回所有老人的健康风险警告")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "返回健康风险警告列表")
    })
    @GetMapping("/list")
    public R<List<AiHealthWarningVO>> getHealthWarnings() {
        // 获取所有健康预警
        List<AiHealthWarning> aiHealthWarnings = aiHealthWarningService.list();

        // 转换为VO对象，并设置老人姓名
        List<AiHealthWarningVO> voList = new ArrayList<>();
        for (AiHealthWarning warning : aiHealthWarnings) {
            AiHealthWarningVO vo = new AiHealthWarningVO();
            BeanUtils.copyProperties(warning, vo);

            // 获取老人信息并设置姓名
            if (warning.getElderlyId() != null) {
                Elderly elderly = elderlyService.getById(warning.getElderlyId());
                if (elderly != null) {
                    vo.setElderlyName(elderly.getName());
                }
            }

            voList.add(vo);
        }

        return R.success(voList);
    }
}
